using UnityEngine;

[DisallowMultipleComponent]
public class AmmoHitEffect : MonoBehaviour
{
    private ParticleSystem ammoHitEffectParticleSystem;

    private void Awake()
    {
        ammoHitEffectParticleSystem = GetComponent<ParticleSystem>();
    }

    /// <summary>
    /// 设置 AmmoHitEffect from 传入的 AmmoHitEffectSO 详细信息
    /// </summary>
    public void SetHitEffect(AmmoHitEffectSO ammoHitEffect)
    {
        // 设置命中效果颜色渐变
        SetHitEffectColorGradient(ammoHitEffect.colorGradient);

        // 设置命中效果粒子系统的起始值
        SetHitEffectParticleStartingValues(ammoHitEffect.duration, ammoHitEffect.startParticleSize, ammoHitEffect.startParticleSpeed, ammoHitEffect.startLifetime, ammoHitEffect.effectGravity, ammoHitEffect.maxParticleNumber);

        // 设置命中效果粒子系统粒子爆发粒子数
        SetHitEffectParticleEmission(ammoHitEffect.emissionRate, ammoHitEffect.burstParticleNumber);

        // Set hit effect particle sprite
        SetHitEffectParticleSprite(ammoHitEffect.sprite);

        // 设置命中效果、生存期、最小和最大速度
        SetHitEffectVelocityOverLifeTime(ammoHitEffect.velocityOverLifetimeMin, ammoHitEffect.velocityOverLifetimeMax);

    }

    /// <summary>
    /// 设置命中效果粒子系统颜色渐变
    /// </summary>
    private void SetHitEffectColorGradient(Gradient gradient)
    {
        // 设置颜色渐变
        ParticleSystem.ColorOverLifetimeModule colorOverLifetimeModule = ammoHitEffectParticleSystem.colorOverLifetime;
        colorOverLifetimeModule.color = gradient;
    }


    /// <summary>
    /// 设置命中效果粒子系统的起始值
    /// </summary>
    private void SetHitEffectParticleStartingValues(float duration, float startParticleSize, float startParticleSpeed, float startLifetime, float effectGravity, int maxParticles)
    {
        ParticleSystem.MainModule mainModule = ammoHitEffectParticleSystem.main;

        // 设置粒子系统持续时间
        mainModule.duration = duration;

        // Set particle start size
        mainModule.startSize = startParticleSize;

        // 设置粒子启动速度
        mainModule.startSpeed = startParticleSpeed;

        // 设置粒子启动生存期
        mainModule.startLifetime = startLifetime;

        // 设置粒子起始重力
        mainModule.gravityModifier = effectGravity;

        // 设置最大粒子数
        mainModule.maxParticles = maxParticles;
    }

    /// <summary>
    /// 设置命中效果粒子系统粒子爆发粒子数
    /// </summary>
    private void SetHitEffectParticleEmission(int emissionRate, float burstParticleNumber)
    {
        ParticleSystem.EmissionModule emissionModule = ammoHitEffectParticleSystem.emission;

        // 设置粒子爆发次数
        ParticleSystem.Burst burst = new ParticleSystem.Burst(0f, burstParticleNumber);
        emissionModule.SetBurst(0, burst);

        // 设置粒子发射率
        emissionModule.rateOverTime = emissionRate;
    }

    /// <summary>
    /// 设置命中效果粒子系统精灵
    /// </summary>
    private void SetHitEffectParticleSprite(Sprite sprite)
    {
        // 设置粒子爆发次数
        ParticleSystem.TextureSheetAnimationModule textureSheetAnimationModule = ammoHitEffectParticleSystem.textureSheetAnimation;

        textureSheetAnimationModule.SetSprite(0, sprite);
    }

    /// <summary>
    /// 设置生命周期内的命中效果速度
    /// </summary>
    private void SetHitEffectVelocityOverLifeTime(Vector3 minVelocity, Vector3 maxVelocity)
    {
        ParticleSystem.VelocityOverLifetimeModule velocityOverLifetimeModule = ammoHitEffectParticleSystem.velocityOverLifetime;

        // 定义最小最大 X 速度
        ParticleSystem.MinMaxCurve minMaxCurveX = new ParticleSystem.MinMaxCurve();
        minMaxCurveX.mode = ParticleSystemCurveMode.TwoConstants;
        minMaxCurveX.constantMin = minVelocity.x;
        minMaxCurveX.constantMax = maxVelocity.x;
        velocityOverLifetimeModule.x = minMaxCurveX;

        // 定义最小最大 Y 速度
        ParticleSystem.MinMaxCurve minMaxCurveY = new ParticleSystem.MinMaxCurve();
        minMaxCurveY.mode = ParticleSystemCurveMode.TwoConstants;
        minMaxCurveY.constantMin = minVelocity.y;
        minMaxCurveY.constantMax = maxVelocity.y;
        velocityOverLifetimeModule.y = minMaxCurveY;

        // 定义最小最大 Z 速度
        ParticleSystem.MinMaxCurve minMaxCurveZ = new ParticleSystem.MinMaxCurve();
        minMaxCurveZ.mode = ParticleSystemCurveMode.TwoConstants;
        minMaxCurveZ.constantMin = minVelocity.z;
        minMaxCurveZ.constantMax = maxVelocity.z;
        velocityOverLifetimeModule.z = minMaxCurveZ;

    }

}